<?php
/**
 * Created by PhpStorm.
 * User: liuwei
 * Date: 2022/12/16
 * Time: 14:19
 */

namespace App\Http\Controllers\Api;


use App\Services\Member\Models\User;
use App\Services\Member\Models\UserCredit;
use App\Services\Member\Models\UserCreditRecord;
use App\Services\Business\Models\Product;
use App\Services\Other\Models\UserPrize;
use App\Services\Member\Services\UserService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Services\Member\Interfaces\UserServiceInterface;
use Illuminate\Support\Facades\DB;


class ProductController extends BaseController
{

    /**
     * @var UserService
     */
    protected $userService;
    public function __construct(
        UserServiceInterface     $userService
    )
    {
        $this->userService = $userService;
    }

    public function index(Request $request)
    {
        $size = $request->input('size', 20);
        $keywords = $request->input('keywords', '');
        $products = Product::query()
            ->when($keywords, function ($query) use ($keywords) {
                return $query->where('title', 'like', '%' . $keywords . '%');
            })
            ->orderByDesc('id')
            ->paginate($size);

        return $this->successData(compact('products'));
    }



    /**
     * 奖品的详情
     * @param Request $request
     * @param $id
     * @return JsonResponse
     */
    public function detail(Request $request, $id)
    {

        $product = Product::query()
            ->with(['images','video'])
            ->where('id', $id)
            ->firstOrFail();
        return $this->successData(compact("product"));

    }


    /**
     * 奖品的置换
     * @param Request $request
     * @return JsonResponse
     */
    public function redeem(Request $request)
    {

        $productId = $request->input('product_id');
        $quantity = $request->input('quantity');

        $product = Product::find($productId);

        // 检查用户的积分余额是否足够兑换指定的商品
        $user = Auth::user();
        $credit  = UserCredit::query()->where('user_id',$user['id'])->where('company_id',$product['company_id'])->first();
        $requiredPoints = $product->point * $quantity;
        //todo 暂时只处理学习的积分
        if ($credit->credit_study< $requiredPoints) {
            return $this->error(__('积分不够兑换,继续努力'));
        }

        DB::transaction(function () use ($user, $credit,$requiredPoints,$productId,$quantity,$product) {

            $credit->increment('credit_study', $requiredPoints);

            UserCreditRecord::create([
                'user_id' => $user['id'],
                'field' => 'credit1',
                'sum' => $requiredPoints,
                'remark' => '奖品置换',
            ]);
            UserPrize::create([
                'user_id' => Auth::id(),
                'product_id' => $productId,
                'quantity' => $quantity,
                'point'=>$product->point,
                'remark'=>'积分兑换'
            ]);
            //更新产品库存
            $product->decrement('stock',$quantity);

        });

        // 返回成功响应
        return $this->success(__('奖品兑换成功'));
    }





}